
package JET.SA;

import JET.Constants;
import JET.Centers.ControlCenter;
import JET.Centers.Graphics.GUICenter.UtilTabs;
import JET.Scene;
import JET.UtilPanels.*;
import JET.geom.Vec2d;
import JET.physics.bodies.SpringBody;
import JET.physics.steering.Torque;
import java.util.ArrayList;

/**
 *
 * @author 057Ry
 */
public class SA_Dialogs implements Constants {

    static final ControlCenter CC = ControlCenter.getInstance();

    ////////////////////////////////////////////////////////////////////////////
    // Dialogs data filling - Dont know how to call this part other way... ;] //
    ////////////////////////////////////////////////////////////////////////////

    /**
     * Fills properties dialog stuff with data from selected masses.
     */
    public static void fillPropertiesDialogWithMassData(Scene scene) {
        double mass = 0.;
        Vec2d velocity = new Vec2d();
        Vec2d position = new Vec2d();
        boolean allTheSame = true;
        UPMass upMass = (UPMass) CC.GUIC.getUtilTab(UtilTabs.Mass);
        UPSpring upSpring = (UPSpring) CC.GUIC.getUtilTab(UtilTabs.Spring);
        UPSpringBody upSpringBody = (UPSpringBody) CC.GUIC.getUtilTab(UtilTabs.SpringBody);

        // -------------------------- Masses tab -------------------------------
        // Setting mass
        if (!scene.EC.selectionGroup.getMassList().isEmpty()) {
            for (int i = 0; i < scene.EC.selectionGroup.getMassList().size(); i++) {
                if (i > 0 && mass != scene.EC.selectionGroup.getMassList().get(i).getMass()) {
                    allTheSame = false;
                    break;
                }
                mass = scene.EC.selectionGroup.getMassList().get(i).getMass();
            }
        }

        if (allTheSame) {
            upMass.setMassGUIValue(mass);
        } else {
            upMass.setMassText(DIFFERENT_VALUES_TEXT);
        }

        // Velocity
        allTheSame = true;
        if (!scene.EC.selectionGroup.getMassList().isEmpty()) {
            for (int i = 0; i < scene.EC.selectionGroup.getMassList().size(); i++) {
                if (i > 0 && !velocity.equals(scene.EC.selectionGroup.getMassList().get(i).getVelocity())) {
                    allTheSame = false;
                    break;
                }
                velocity.become(scene.EC.selectionGroup.getMassList().get(i).getVelocity());
            }
        }

        if (allTheSame) {
            upMass.setVelocityGUIValue(velocity);
        } else {
            upMass.setVelocityGUIValue(new Vec2d(0., 0.));
            upMass.setVelocityText(DIFFERENT_VALUES_TEXT);
        }

        // Position
        allTheSame = true;
        if (!scene.EC.selectionGroup.getMassList().isEmpty()) {
            for (int i = 0; i < scene.EC.selectionGroup.getMassList().size(); i++) {
                if (i > 0 && !position.equals(scene.EC.selectionGroup.getMassList().get(i).getPosition())) {
                    allTheSame = false;
                    break;
                }
                position.become(scene.EC.selectionGroup.getMassList().get(i).getPosition());
            }
        }

        if (allTheSame) {
            upMass.setPositionGUIValue(position.getX(), position.getY());
        } else {
            upMass.setPositionText(DIFFERENT_VALUES_TEXT);
        }

        // ------------------------ Spring Bodies tab --------------------------
        upSpringBody.setSpringBodyShapeMatchingActivatedCheckBoxEnabled(!scene.EC.selectionGroup.getMassList().isEmpty());
    }

    /**
     * Fills properties dialog stuff with data from selected springs.
     */
    public static void fillPropertiesDialogWithSpringData(Scene scene) {
        double ks = 0., kd = 0., kt = 0., startLength = 0.;
        String name = "";
        boolean tearable = false, alive = false;
        boolean allTheSame = true;
        UPSpring upSpring = (UPSpring) CC.GUIC.getUtilTab(UtilTabs.Spring);

        // Name
        allTheSame = true;
        if (!scene.EC.selectionGroup.getSpringList().isEmpty()) {
            for (int i = 0; i < scene.EC.selectionGroup.getSpringList().size(); i++) {
                if (i > 0 && !name.equals(scene.EC.selectionGroup.getSpringList().get(i).getName())) {
                    allTheSame = false;
                    break;
                }
                name = scene.EC.selectionGroup.getSpringList().get(i).getName();
            }
        }

        if (allTheSame) {
            upSpring.setSpringsNameGUIValue(name);
        } else {
            upSpring.setSpringsNameGUIValue(DIFFERENT_VALUES_TEXT);
        }

        // Ks constant
        allTheSame = true;
        if (!scene.EC.selectionGroup.getSpringList().isEmpty()) {
            for (int i = 0; i < scene.EC.selectionGroup.getSpringList().size(); i++) {
                if (i > 0 && ks != scene.EC.selectionGroup.getSpringList().get(i).getKs()) {
                    allTheSame = false;
                    break;
                }
                ks = scene.EC.selectionGroup.getSpringList().get(i).getKs();
            }
        }

        if (allTheSame) {
            upSpring.setSpringsKsGUIValue(ks);
        } else {
            upSpring.setSpringsKsText(DIFFERENT_VALUES_TEXT);
        }

        // Kd constant
        allTheSame = true;
        if (!scene.EC.selectionGroup.getSpringList().isEmpty()) {
            for (int i = 0; i < scene.EC.selectionGroup.getSpringList().size(); i++) {
                if (i > 0 && kd != scene.EC.selectionGroup.getSpringList().get(i).getKd()) {
                    allTheSame = false;
                    break;
                }
                kd = scene.EC.selectionGroup.getSpringList().get(i).getKd();
            }
        }

        if (allTheSame) {
            upSpring.setSpringsKdGUIValue(kd);
        } else {
            upSpring.setSpringsKdText(DIFFERENT_VALUES_TEXT);
        }

        // Kt constant
        allTheSame = true;
        if (!scene.EC.selectionGroup.getSpringList().isEmpty()) {
            for (int i = 0; i < scene.EC.selectionGroup.getSpringList().size(); i++) {
                if (i > 0 && kt != scene.EC.selectionGroup.getSpringList().get(i).getKt()) {
                    allTheSame = false;
                    break;
                }
                kt = scene.EC.selectionGroup.getSpringList().get(i).getKt();
            }
        }

        if (allTheSame) {
            upSpring.setSpringsKtGUIValue(kt);
        } else {
            upSpring.setSpringsKtText(DIFFERENT_VALUES_TEXT);
        }

        // Tearable
        allTheSame = true;
        if (!scene.EC.selectionGroup.getSpringList().isEmpty()) {
            for (int i = 0; i < scene.EC.selectionGroup.getSpringList().size(); i++) {
                if (i > 0 && tearable != scene.EC.selectionGroup.getSpringList().get(i).isTearable()) {
                    allTheSame = false;
                    break;
                }
                tearable = scene.EC.selectionGroup.getSpringList().get(i).isTearable();
            }
        }

        if (allTheSame) {
            upSpring.setSpringsTearableGUIValue(tearable);
        } else {
            upSpring.setSpringsTearableGUIValue(false);
        }

        // Alive
        allTheSame = true;
        if (!scene.EC.selectionGroup.getSpringList().isEmpty()) {
            for (int i = 0; i < scene.EC.selectionGroup.getSpringList().size(); i++) {
                if (i > 0 && tearable != scene.EC.selectionGroup.getSpringList().get(i).isAlive()) {
                    allTheSame = false;
                    break;
                }
                alive = scene.EC.selectionGroup.getSpringList().get(i).isAlive();
            }
        }

        if (allTheSame) {
            upSpring.setSpringsAliveGUIValue(tearable);
        } else {
            upSpring.setSpringsAliveGUIValue(false);
        }

        // Start length
        allTheSame = true;
        if (!scene.EC.selectionGroup.getSpringList().isEmpty()) {
            for (int i = 0; i < scene.EC.selectionGroup.getSpringList().size(); i++) {
                if (i > 0 && startLength != scene.EC.selectionGroup.getSpringList().get(i).getStartLength()) {
                    allTheSame = false;
                    break;
                }
                startLength = scene.EC.selectionGroup.getSpringList().get(i).getStartLength();
            }
        }

        if (allTheSame) {
            upSpring.setStartLengthGUIValue(startLength);
        } else {
            upSpring.setStartLengthText(DIFFERENT_VALUES_TEXT);
        }
    }

    /**
     * Fills properties dialog stuff with data from selected springs.
     */
    public static void fillPropertiesDialogWithRSpringData(Scene scene) {
        double ks = 0., kd = 0., startAngle = 0.;
        boolean allTheSame = true;
        UPRSpring upRSpring = (UPRSpring) CC.GUIC.getUtilTab(UtilTabs.RSpring);

        // Ks constant
        if (!scene.EC.selectionGroup.getRSpringList().isEmpty()) {
            for (int i = 0; i < scene.EC.selectionGroup.getRSpringList().size(); i++) {
                if (i > 0 && ks != scene.EC.selectionGroup.getRSpringList().get(i).getKs()) {
                    allTheSame = false;
                    break;
                }
                ks = scene.EC.selectionGroup.getRSpringList().get(i).getKs();
            }
        }

        if (allTheSame) {
            upRSpring.setRSpringsKsGUIValue(ks);
        } else {
            upRSpring.setRSpringsKsText(DIFFERENT_VALUES_TEXT);
        }
    
        // Kd constant
        allTheSame = true;
        if (!scene.EC.selectionGroup.getRSpringList().isEmpty()) {
            for (int i = 0; i < scene.EC.selectionGroup.getRSpringList().size(); i++) {
                if (i > 0 && kd != scene.EC.selectionGroup.getRSpringList().get(i).getKd()) {
                    allTheSame = false;
                    break;
                }
                kd = scene.EC.selectionGroup.getRSpringList().get(i).getKd();
            }
        }

        if (allTheSame) {
            upRSpring.setRSpringsKdGUIValue(kd);
        } else {
            upRSpring.setRSpringsKdText(DIFFERENT_VALUES_TEXT);
        }

        // Start Angle
        allTheSame = true;
        if (!scene.EC.selectionGroup.getRSpringList().isEmpty()) {
            for (int i = 0; i < scene.EC.selectionGroup.getRSpringList().size(); i++) {
                if (i > 0 && startAngle != scene.EC.selectionGroup.getRSpringList().get(i).getStartAngle()) {
                    allTheSame = false;
                    break;
                }
                startAngle = scene.EC.selectionGroup.getRSpringList().get(i).getStartAngle();
            }
        }

        if (allTheSame) {
            upRSpring.setStartAngleGUIValue(Math.toDegrees(startAngle));
        } else {
            upRSpring.setStartAngleText(DIFFERENT_VALUES_TEXT);
        }
    }

    /**
     * Fills properties dialog stuff with data from actual spring body.
     */
    public static void fillPropertiesDialogWithSpringBodyData(SpringBody sb) {
        UPSpringBody upSpringBody = (UPSpringBody) CC.GUIC.getUtilTab(UtilTabs.SpringBody);
        if (upSpringBody != null && sb != null) {
            upSpringBody.setSpringBodyNameText(sb.getName());
            upSpringBody.setSpringBodyMaterialIndex(sb.getMaterial());
        }
    }

    /**
     * Fills properties dialog stuff with data from selected torques
     * @param tList
     */
    public static void fillPropertiesDialogWithTorquesData(ArrayList<Torque> tList) {
        if (tList.size() <= 0) {
            return;
        }

        UPTorque upTorque = (UPTorque) CC.GUIC.getUtilTab(UtilTabs.Torque);

        boolean allTheSame = true;

        // Name
        String name = tList.get(0).getName();
        for (Torque t : tList) {
            if (!name.equals(t.getName())) {
                allTheSame = false;
                break;
            }
        }

        if (allTheSame) {
            upTorque.setTorqueNameTextFieldValue(name);
        } else {
            upTorque.setTorqueNameTextFieldValue(Constants.DIFFERENT_VALUES_TEXT);
        }

        // Strength
        allTheSame = true;
        double strength = 0.;
        strength = tList.get(0).getValue();
        for (Torque t : tList) {
            if (strength != t.getValue()) {
                allTheSame = false;
                break;
            }
        }

        if (allTheSame) {
            upTorque.setTorqueStrengthTextFieldValue("" + strength);
        } else {
            upTorque.setTorqueStrengthTextFieldValue(Constants.DIFFERENT_VALUES_TEXT);
        }

        // Activated
        allTheSame = true;
        boolean activated = tList.get(0).isActive();
        for (Torque t : tList) {
            if (t.isActive() != activated) {
                allTheSame = false;
                break;
            }
        }

        if (allTheSame) {
            upTorque.setTorqueActiveCheckBoxValue(activated);
        }
        //else
        //  CC.GUIC.propertiesDialog.setTorqueActiveCheckBoxValue(false);
    }

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     * Dialogs - showing/hiding
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    
    public static void setVisibleMaterialsDialog(boolean visible) {
        switch (CC.runMode) {
            case Desktop : CC.GUIC.getMainFrame().getShowMaterialsDialogCheckBoxMenuItem().setSelected(visible); break;
            case Applet  : break;
        }
        CC.GUIC.materialsDialog.setVisible(visible);
    }

    public static void setVisibleActionsDialog(boolean visible) {
        switch (CC.runMode) {
            case Desktop : CC.GUIC.getMainFrame().getShowActionsDialogCheckBoxMenuItem().setSelected(visible); break;
            case Applet  : break;
        }
        CC.GUIC.actionsDialog.setVisible(visible);
    }

    public static void setVisibleKeyboardDialog(boolean visible) {
        switch (CC.runMode) {
            case Desktop : CC.GUIC.getMainFrame().getShowKeyboardDialogCheckBoxMenuItem().setSelected(visible); break;
            case Applet  : break;
        }
        CC.GUIC.keyboadDialog.setVisible(visible);
    }

    public static void setVisibleRecorderDialog(boolean visible) {
        switch (CC.runMode) {
            case Desktop : CC.GUIC.getMainFrame().getShowRecorderDialogCheckBoxMenuItem().setSelected(visible); break;
            case Applet  : break;
        }
        CC.GUIC.recorderDialog.setVisible(visible);
    }

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     * Dialogs - options dialog panels management
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    //public static void
}